#include "NeoPushButtonPrivate.h"
#include "NeoPushButton.h"

NeoPushButtonPrivate::NeoPushButtonPrivate(QObject *parent)
    : QObject(parent),
      pBorderRadius(5),
      pNormalBackgroundColor(Qt::white),
      pHoverBackgroundColor(Qt::lightGray),
      pPressedBackgroundColor(Qt::darkGray),
      pButtonStyle(NeoPushButtonType::ButtonTextUnderIcon)
{
}

NeoPushButtonPrivate::~NeoPushButtonPrivate() = default;

void NeoPushButtonPrivate::drawBackground(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoPushButton);
    painter->save();
    painter->setBrush(q->isDown() ? pPressedBackgroundColor : (q->underMouse() ? pHoverBackgroundColor : pNormalBackgroundColor));
    painter->drawRoundedRect(q->rect().adjusted(NEO_RADIUS_ADJUSTED_VALUES), pBorderRadius, pBorderRadius);
    painter->restore();
}

void NeoPushButtonPrivate::drawIcon(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoPushButton);
    if (q->icon().isNull())
    {
        return;
    }
    painter->save();
    QPixmap pixmap = q->icon().pixmap(q->iconSize());
    switch (pButtonStyle)
    {
    case NeoPushButtonType::ButtonTextBesideIcon:
    {
        QRect iconRect = q->rect();
        iconRect.setSize(pixmap.size());
        iconRect.moveCenter(q->rect().center());
        QString elidedText = q->fontMetrics().elidedText(q->text(), Qt::ElideRight, q->width() - pixmap.width() - pSpace);
        iconRect.moveLeft(iconRect.left() - q->fontMetrics().horizontalAdvance(elidedText) / 2);
        painter->drawPixmap(iconRect, pixmap);
        break;
    }
    case NeoPushButtonType::ButtonTextUnderIcon:
    {
        QRect iconRect = q->rect();
        iconRect.setSize(pixmap.size());
        iconRect.moveCenter(q->rect().center());
        iconRect.moveTop(iconRect.top() - q->fontMetrics().height() / 2);
        painter->drawPixmap(iconRect, pixmap);
        break;
    }
    case NeoPushButtonType::ButtonIconOnly:
    {
        QRect iconRect = q->rect();
        iconRect.setSize(pixmap.size());
        iconRect.moveCenter(q->rect().center());
        painter->drawPixmap(iconRect, pixmap);
        break;
    }
    case NeoPushButtonType::ButtonTextOnly:
    default:
        break;
    }
    painter->restore();
}

void NeoPushButtonPrivate::drawText(QPainter *painter)
{
    NEO_PRIVATE_POINT(NeoPushButton);
    if (q->text().isEmpty())
    {
        return;
    }
    painter->save();
    painter->setPen(q->palette().color(QPalette::ButtonText));
    painter->setFont(q->font());
    switch (pButtonStyle)
    {
    case NeoPushButtonType::ButtonTextBesideIcon:
    {
        QRect textRect = q->rect();
        textRect.moveCenter(q->rect().center());
        QString elidedText = q->fontMetrics().elidedText(q->text(), Qt::ElideRight, q->width() - q->iconSize().width() - pSpace);
        textRect.moveLeft(textRect.left() + q->fontMetrics().horizontalAdvance(elidedText) / 2);
        painter->drawText(textRect, Qt::AlignCenter, elidedText);
        break;
    }
    case NeoPushButtonType::ButtonTextUnderIcon:
    {
        QRect textRect = q->rect();
        textRect.moveCenter(q->rect().center());
        QString elidedText = q->fontMetrics().elidedText(q->text(), Qt::ElideMiddle, q->width() - 2 * pSpace);
        textRect.moveTop(textRect.top() + q->iconSize().height() / 2);
        painter->drawText(textRect, Qt::AlignCenter, elidedText);
        break;
    }
    case NeoPushButtonType::ButtonTextOnly:
    {
        QRect textRect = q->rect();
        QString elidedText = q->fontMetrics().elidedText(q->text(), Qt::ElideMiddle, q->width() - 2 * pSpace);
        painter->drawText(textRect, Qt::AlignCenter, elidedText);
        break;
    }
    case NeoPushButtonType::ButtonIconOnly:
    default:
        break;
    }
    painter->restore();
}